<template>
  <div :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  props: {
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "500px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.initChart();
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize();
        }
      }, 100);
      window.addEventListener("resize", this.__resizeHandler);
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName("sidebar-container")[0];
    this.sidebarElm &&
      this.sidebarElm.addEventListener(
        "transitionend",
        this.sidebarResizeHandler
      );
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    if (this.autoResize) {
      window.removeEventListener("resize", this.__resizeHandler);
    }

    this.sidebarElm &&
      this.sidebarElm.removeEventListener(
        "transitionend",
        this.sidebarResizeHandler
      );

    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === "width") {
        this.__resizeHandler();
      }
    },
    setOptions({ expectedData, actualData } = {}) {
      const series = [];
      let ele = [];
      const legendData = [];
      for (let i = 0; i < this.chartData.barData.length; i++) {
        ele = this.chartData.barData[i];
        legendData.push(ele.name);
        series.push({
          name: ele.name,
          smooth: true,
          type: "line",
          label: {
            show: true, // 开启显示
            position: "top", // 在上方显示
            fontSize: 12
          },
          data: ele.num,
          animationDuration: 2800,
          animationEasing: "quadraticOut"
        });
      }
      // this.chart.setOption(option, true);
      this.chart.setOption({
        grid: {
          // 设置直角坐标系
          top: 50,
          left: "2%",
          right: "2%",
          bottom: "4%",
          containLabel: true
        },
        tooltip: {
          // 设置提示框
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10]
        },
        toolbox: {
          // 设置工具栏
          show: true,
          right: "2%",
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ["line", "bar"] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: {
          // 设置直角坐标系 grid 中的 x 轴
          data: ele.content,
          boundaryGap: false,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            interval: 0,
            formatter: function(params, index) {
              if (index % 2 != 0) {
                return "\n\n" + params;
              } else {
                return params;
              }
            }
          }
        },
        yAxis: {
          // 设置直角坐标系 grid 中的 y 轴
          axisTick: {
            show: false
          }
        },
        legend: {
          // 设置图例
          data: legendData
        },
        dataZoom: [
          // 设置区域缩放
          {
            type: "inside"
          },
          {
            type: "slider"
          }
        ],
        series: series // 设置系列列表(图表类型相关)
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    }
  }
};
</script>
